package com.forestar.mapControl.mapview.multitouch.listener;

import android.graphics.PointF;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.Scroller;

import com.forestar.mapControl.utils.LogD;


/**
 * A custom gesture detector that processes gesture events and dispatches them
 * to the map's overlay system.
 */
public class MapViewGestureDetectorListener extends SimpleOnGestureListener
  {

    public interface OnScrollerListener{
      void onStartScroll(float x,float y);
      void onStopScroll(float x,float y);
    }

    private OnScrollerListener scrollerListener;

    private Scroller mScroller;
//    private final MapControl mapControl;

    /**
     * Bind a new gesture detector to a map
     */
    public MapViewGestureDetectorListener(final Scroller mScroller,OnScrollerListener scrollerListener)
      {
        this.mScroller = mScroller;
        this.scrollerListener = scrollerListener;
      }

    @Override
    public boolean onDown(final MotionEvent e)
      {
        try
          {
            if(null != mScroller && !mScroller.isFinished()){
              //停止滑动计算
              mScroller.abortAnimation();
              //在这里，初始化滚动点
              if(null != this.scrollerListener){
                this.scrollerListener.onStopScroll(e.getX(),e.getY());
              }
            }
            LogD.e("按下停止键:"+mScroller.isFinished());
          } catch (Exception ex)
          {
          }
        return true;
      }

    @Override
    public boolean onFling(final MotionEvent motionEvent1, final MotionEvent motionEvent2, final float velocityX, final float velocityY)
      {
        try
          {
            LogD.e("滑动fly:"+motionEvent1.getX());
            LogD.e("滑动fly:"+velocityX);
//            mScroller.startScroll((int) motionEvent1.getX(),((int) motionEvent1.getY()), ((int)velocityX), ((int)velocityY));
            mScroller.fling(((int) motionEvent1.getX()), ((int) motionEvent1.getY())
                    , ((int)velocityX), ((int)velocityY),
                    Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
            //在这里记录下，惯性滚动的第一个起始点
            if(null != this.scrollerListener){
              this.scrollerListener.onStartScroll(  motionEvent1.getX(),motionEvent1.getY());
            }
//            if (this.mapControl.isAnimating() || this.mapControl.getGestureListenerManager().onFling(e1, e2, velocityX, velocityY, this.mapControl))
//              {
//                return true;
//              }
//            this.mapControl.mIsFlinging = true;
          } catch (Exception ex)
          {
          }
        return true;
      }

    @Override
    public void onLongPress(final MotionEvent e)
      {
        try
          {
//            if (this.mapControl.getGestureListenerManager().onLongPress(e, this.mapControl))
//              {
//                return;
//              }
          } catch (Exception ex)
          {
          }

      }

    @Override
    public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY)
      {
        try
          {
//            if (this.mapControl.getGestureListenerManager().onScroll(e1, e2, distanceX, distanceY, this.mapControl))
//              {
//                return true;
//              }
          } catch (Exception ex)
          {
          }
        return false;
      }

    @Override
    public void onShowPress(final MotionEvent e)
      {
        try
          {
//            this.mapControl.getGestureListenerManager().onShowPress(e, this.mapControl);
          } catch (Exception ex)
          {
          }

      }

    @Override
    public boolean onSingleTapUp(final MotionEvent e)
      {
        try
          {
//            return this.mapControl.getGestureListenerManager().onSingleTapUp(e, this.mapControl);
          } catch (Exception ex)
          {
          }
        return true;
      }

    @Override
    public boolean onSingleTapConfirmed(final MotionEvent e)
      {
        try
          {
          } catch (Exception ex)
          {
          }
//        return this.mapControl.getGestureListenerManager().onSingleTapConfirmed(e, this.mapControl);
        return false;
      }

    @Override
    public boolean onDoubleTap(final MotionEvent e)
      {
        try
          {
//            if (mapControl.getCurrentTool() == null)
//              {
//                PointF point = new PointF(e.getX(), e.getY());
//                this.mapControl.ZoomInAt(point);
//              }
//            this.mapControl.getGestureListenerManager().onDoubleTap(e, this.mapControl);
          } catch (Exception ex)
          {
          }
        return true;
      }
  }
